using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Common;
using SExperiment;

namespace MainView.Windows
{
	public partial class SetBadPointWindow : Window, IComponentConnector
	{
		public delegate void DelegateOnEvent(object sender, EventArgs e);

		public static readonly DependencyProperty MarkBadPointProperty = DependencyProperty.Register("MarkBadPoint", typeof(bool), typeof(SetBadPointWindow), new PropertyMetadata(false));

		public static readonly DependencyProperty DataListProperty = DependencyProperty.Register("DataList", typeof(List<KeyValuePair>), typeof(SetBadPointWindow), new PropertyMetadata(new List<KeyValuePair>()));

		private static string STR_Raw_AMP = Utility.GetMessage("raw_amplification_program");

		private static string STR_Raw_Melt = Utility.GetMessage("raw_melting_curve");

		private Hashtable _HTtBadPoint = new Hashtable();

		private List<double> _RawList;

		private List<double> _RawMeltList;

		private List<BadPoint> selectedPoint = new List<BadPoint>();

		private List<Well> _SelectedWells;

		public bool MarkBadPoint
		{
			get
			{
				return (bool)GetValue(MarkBadPointProperty);
			}
			set
			{
				SetValue(MarkBadPointProperty, value);
			}
		}

		public List<KeyValuePair> DataList
		{
			get
			{
				return (List<KeyValuePair>)GetValue(DataListProperty);
			}
			set
			{
				SetValue(DataListProperty, value);
			}
		}

		public event DelegateOnEvent OnPreview;

		public event DelegateOnEvent OnApply;

		public SetBadPointWindow(Hashtable htBadPoint, List<double> rawList, List<double> rawMeltList)
		{
			_HTtBadPoint = htBadPoint;
			_RawList = rawList;
			_RawMeltList = rawMeltList;
			InitializeComponent();
			if (_RawList != null && _RawList.Count > 0)
			{
				cmbType.Items.Add(STR_Raw_AMP);
			}
			if (_RawMeltList != null && _RawMeltList.Count > 0)
			{
				cmbType.Items.Add(STR_Raw_Melt);
			}
			cmbType.SelectedIndex = 0;
		}

		private void cmbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			List<KeyValuePair> list = new List<KeyValuePair>();
			list.Add(new KeyValuePair
			{
				Key = "*",
				Value = -2
			});
			list.Add(new KeyValuePair
			{
				Key = "N/A",
				Value = -1
			});
			if (GetTypeName() == STR_Raw_AMP)
			{
				for (int i = 0; i < _RawList.Count; i++)
				{
					list.Add(new KeyValuePair
					{
						Key = _RawList[i].ToString("0"),
						Value = i
					});
				}
			}
			else if (GetTypeName() == STR_Raw_Melt)
			{
				for (int j = 0; j < _RawMeltList.Count; j++)
				{
					list.Add(new KeyValuePair
					{
						Key = _RawMeltList[j].ToString("0.00"),
						Value = j
					});
				}
			}
			DataList = list;
			SetSelectedWell(_SelectedWells);
		}

		public void SetSelectedWell(List<Well> wells)
		{
			if (wells == null)
			{
				return;
			}
			if (wells != _SelectedWells)
			{
				Restore();
			}
			_SelectedWells = wells;
			selectedPoint.Clear();
			foreach (Well selectedWell in _SelectedWells)
			{
				foreach (SampleTargetItem target in selectedWell.Targets)
				{
					if (target.TubeNo == selectedWell.MultiTubeID)
					{
						BadPoint badPoint = new BadPoint();
						badPoint.WellNo = selectedWell.ID;
						badPoint.ChannelNo = target.ChannelNo;
						BadPoint item = badPoint;
						string key = selectedWell.ID.ToString("00") + target.ChannelNo;
						if (_HTtBadPoint.ContainsKey(key))
						{
							item = ((BadPoint)_HTtBadPoint[key]).Clone();
						}
						selectedPoint.Add(item);
					}
				}
			}
			int i;
			for (i = 1; i <= 6; i++)
			{
				List<BadPoint> list = selectedPoint.Where((BadPoint s) => s.ChannelNo == i).ToList();
				StackPanel stackPanel = wpChannel.Children[i - 1] as StackPanel;
				stackPanel.Visibility = ((list.Count == 0) ? Visibility.Collapsed : Visibility.Visible);
				if (stackPanel.Visibility != 0)
				{
					continue;
				}
				int j;
				for (j = 0; j < 3; j++)
				{
					List<int> list2 = new List<int>();
					if (GetTypeName() == STR_Raw_AMP)
					{
						list2 = list.Select((BadPoint s) => s.RawPoints[j]).Distinct().ToList();
					}
					else if (GetTypeName() == STR_Raw_Melt)
					{
						list2 = list.Select((BadPoint s) => s.RawMeltPoints[j]).Distinct().ToList();
					}
					if (list2.Count > 1)
					{
						((ComboBox)stackPanel.Children[j + 1]).SelectedIndex = 0;
					}
					else if (list2.Count == 1)
					{
						((ComboBox)stackPanel.Children[j + 1]).SelectedValue = list2[0];
					}
					else
					{
						((ComboBox)stackPanel.Children[j + 1]).SelectedIndex = 1;
					}
				}
			}
		}

		private void Restore()
		{
			foreach (BadPoint item in selectedPoint)
			{
				string key = item.WellNo.ToString("00") + item.ChannelNo;
				if (_HTtBadPoint.ContainsKey(key))
				{
					if (!item.IsEmpty())
					{
						_HTtBadPoint[key] = item.Clone();
					}
					else
					{
						_HTtBadPoint.Remove(key);
					}
				}
			}
		}

		private void Root_Closed(object sender, EventArgs e)
		{
			Restore();
		}

		private void btnPreview_Click(object sender, RoutedEventArgs e)
		{
			Preview();
			if (this.OnPreview != null)
			{
				this.OnPreview(this, new EventArgs());
			}
		}

		private void Preview(bool apply = false)
		{
			foreach (BadPoint item in selectedPoint)
			{
				string key = item.WellNo.ToString("00") + item.ChannelNo;
				BadPoint badPoint = null;
				if (!_HTtBadPoint.ContainsKey(key))
				{
					BadPoint badPoint2 = new BadPoint();
					badPoint2.WellNo = item.WellNo;
					badPoint2.ChannelNo = item.ChannelNo;
					badPoint = badPoint2;
					_HTtBadPoint.Add(key, badPoint);
				}
				else
				{
					badPoint = (BadPoint)_HTtBadPoint[key];
				}
				for (int i = 0; i < 3; i++)
				{
					ComboBox comboBox = (ComboBox)((StackPanel)wpChannel.Children[item.ChannelNo - 1]).Children[i + 1];
					if (comboBox.SelectedIndex <= 0)
					{
						continue;
					}
					if (GetTypeName() == STR_Raw_AMP)
					{
						badPoint.RawPoints[i] = (int)comboBox.SelectedValue;
						if (apply)
						{
							item.RawPoints[i] = badPoint.RawPoints[i];
						}
					}
					else if (GetTypeName() == STR_Raw_Melt)
					{
						badPoint.RawMeltPoints[i] = (int)comboBox.SelectedValue;
						if (apply)
						{
							item.RawMeltPoints[i] = badPoint.RawMeltPoints[i];
						}
					}
				}
				if (badPoint.IsEmpty())
				{
					_HTtBadPoint.Remove(key);
				}
			}
		}

		private string GetTypeName()
		{
			if (cmbType.SelectedItem == null)
			{
				return string.Empty;
			}
			return cmbType.SelectedItem.ToString();
		}

		private void btnApply_Click(object sender, RoutedEventArgs e)
		{
			Preview(true);
			if (this.OnApply != null)
			{
				this.OnApply(this, new EventArgs());
			}
		}

		private void btnClose_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}
	}
}
